export interface ResDataType {
    lv: number,
    must: boolean,
    name: string,
    note: string,
    type: string,
    value: string,
}

interface stackMem {
    level: number,
    val: number,
}
export const transfer = (arr: ResDataType[]) => {
    let stack: stackMem[] = [];
    let obj: stackMem;
    let colon = ':';
    let singleQuotes = '"';
    let lineFeed = '\n';
    let comma = ','
    let space = '    '
    let braceR = '}';
    let bracketR = ']';
    let flag = 0;
    let braceL = (level: number) => {
        obj = {
            val: 1,
            level: level,
        }
        stack.push(obj);

        return '{\n';
    }
    let bracketL = (level: number) => {
        obj = {
            val: 0,
            level: level,
        }
        stack.push(obj);

        return '[\n';
    }
    //初始化
    let valString = '{\n'
    // 遍历
    if (arr.length === 1) {
        if (arr[0].type === "object") {
            valString += space.repeat(arr[0].lv - 1) + arr[0].name + colon + '{}' + comma + lineFeed;
        } else if (arr[0].type === "array") {
            valString += space.repeat(arr[0].lv - 1) + arr[0].name + colon + '[]' + comma + lineFeed;
        } else {
            valString += space.repeat(arr[0].lv - 1) + arr[0].name + colon + singleQuotes + arr[flag].value + singleQuotes + comma + lineFeed;
        }
    } else {
        while (flag + 1 < arr.length) {
            if (arr[flag].lv === arr[flag + 1].lv) {
                if (arr[flag].name === '') {
                    if (arr[flag].type === "object") {
                        valString += space.repeat(arr[flag].lv - 1) + '{}' + comma + lineFeed;
                    } else if (arr[flag].type === "array") {
                        valString += space.repeat(arr[flag].lv - 1) + '[]' + comma + lineFeed;
                    } else {
                        valString += space.repeat(arr[flag].lv - 1) + singleQuotes + arr[flag].value + singleQuotes + comma + lineFeed;
                    }
                } else {
                    if (arr[flag].type === "object") {
                        valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + '{}' + comma + lineFeed;
                    } else if (arr[flag].type === "array") {
                        valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + '[]' + comma + lineFeed;
                    } else {
                        valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + singleQuotes + arr[flag].value + singleQuotes + comma + lineFeed;
                    }
                }

            } else if (arr[flag].lv < arr[flag + 1].lv) {
                if (arr[flag].name === '') {
                    if (arr[flag].type === 'object') {
                        valString += space.repeat(arr[flag].lv - 1) + braceL(arr[flag].lv);
                    }
                    if (arr[flag].type === 'array') {
                        valString += space.repeat(arr[flag].lv - 1) + bracketL(arr[flag].lv);
                    }
                } else {
                    if (arr[flag].type === 'object') {
                        valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + braceL(arr[flag].lv);
                    }
                    if (arr[flag].type === 'array') {
                        valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + bracketL(arr[flag].lv);
                    }
                }


            } else {
                // 对于前面大于后面的情况
                // 先生成本次的对象
                let dis = arr[flag].lv - arr[flag + 1].lv;
                let tempObj = stack.pop();
                if (tempObj) {
                    if (tempObj.val) {
                        if (arr[flag].type === "object") {
                            valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + '{}' + lineFeed + space.repeat(tempObj.level - 1) + braceR + comma + lineFeed;
                        } else if (arr[flag].type === "array") {
                            valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + '[]' + lineFeed + space.repeat(tempObj.level - 1) + braceR + comma + lineFeed;
                        } else {
                            valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + singleQuotes + arr[flag].value + singleQuotes + lineFeed + space.repeat(tempObj.level - 1) + braceR + comma + lineFeed;
                        }


                    } else {
                        if (arr[flag].type === "object") {
                            valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + '{}' + lineFeed + space.repeat(tempObj.level - 1) + braceR + comma + lineFeed;
                        } else if (arr[flag].type === "array") {
                            valString += space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + '[]' + lineFeed + space.repeat(tempObj.level - 1) + braceR + comma + lineFeed;
                        } else {
                            valString += space.repeat(arr[flag].lv - 1) + singleQuotes + arr[flag].value + singleQuotes + lineFeed + space.repeat(tempObj.level - 1) + bracketR + comma + lineFeed;
                        }

                    }
                }
                //再配对括号
                for (let i = 0; i < dis - 1; i++) {
                    let tempObj = stack.pop();
                    if (tempObj) {
                        if (tempObj.val) {
                            if (i + 1 === dis - 1) {
                                valString += space.repeat(tempObj.level - 1) + braceR + comma + lineFeed;
                            } else {
                                valString += space.repeat(tempObj.level - 1) + braceR + comma + lineFeed;
                            }


                        } else {
                            if (i + 1 === dis - 1) {
                                valString += space.repeat(tempObj.level - 1) + bracketR + comma + lineFeed;
                            } else {
                                valString += space.repeat(tempObj.level - 1) + bracketR + comma + lineFeed;
                            }


                        }
                    }
                }

            }
            // 最后一个元素的处理
            if (flag + 2 === arr.length) {
                let loc = flag + 1;
                if (arr[loc].name === '') {
                    if (arr[loc].type === 'object') {
                        valString = valString + space.repeat(arr[flag + 1].lv - 1) + '{}' + comma + lineFeed;

                    } else if (arr[loc].type === 'array') {
                        valString = valString + space.repeat(arr[flag + 1].lv - 1) + '[]' + comma + lineFeed;
                    } else {
                        valString = valString + space.repeat(arr[flag + 1].lv - 1) + singleQuotes + arr[flag + 1].value + singleQuotes + comma + lineFeed;
                    }
                } else {
                    if (arr[loc].type === 'object') {
                        valString = valString + space.repeat(arr[flag + 1].lv - 1) + arr[flag + 1].name + colon + '{}' + comma + lineFeed;

                    } else if (arr[loc].type === 'array') {
                        valString = valString + space.repeat(arr[flag + 1].lv - 1) + arr[flag + 1].name + colon + '[]' + comma + lineFeed;
                    } else {
                        valString = valString + space.repeat(arr[flag + 1].lv - 1) + arr[flag + 1].name + colon + singleQuotes + arr[flag + 1].value + singleQuotes + comma + lineFeed;
                    }
                }

            }
            flag += 1;
        }

    }

    // 最后括号配对
    for (let i = 0; i < stack.length + 1; i++) {
        let tempObj = stack.pop();
        if (tempObj) {
            if (tempObj.val) {
                valString += space.repeat(tempObj.level > 0 ? tempObj.level - 1 : 0) + braceR + comma + lineFeed;
            } else {
                valString += space.repeat(tempObj.level > 0 ? tempObj.level - 1 : 0) + bracketR + comma + lineFeed;
            }
        }
    }



    return valString + '}';

}
























// export const transfer = (arr: ResDataType[]) => {
//     let braceL = '{\n';
//     let braceR = '}';
//     let colon = ':';
//     let singleQuotes = '"';
//     let lineFeed = '\n';
//     let comma = ','
//     let bracketL = '[\n';
//     let bracketR = ']';
//     let flag = 0;
//     let space = '    '
//     let stick:number[] = [];
//     let stCou = 0;
//     let valString = braceL + '';
//     while (flag + 1 < arr.length) {
//         if (arr[flag].lv === arr[flag + 1].lv) {
//             console.log("=");
//             valString = valString + space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + singleQuotes + arr[flag].value + singleQuotes + comma + lineFeed;
//         } else if (arr[flag].lv < arr[flag + 1].lv) {
//             console.log("<");
//             if (arr[flag].type === 'object') {
//                 valString = valString + space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + braceL;
//             }
//             if (arr[flag].type === 'array') {
//                 valString = valString + space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + bracketL;
//             }
//         } else if (arr[flag].lv > arr[flag + 1].lv) {
//             console.log(">");
//             let loc = flag;
//             let loc1 = flag;
//             let type = "";
//             stick[stCou] = flag;
//             stCou += 1;
//             while (arr[loc1].lv > arr[flag + 1].lv - 1 && loc1) {
//                 if (arr[loc1].lv < arr[flag].lv) {
//                     type = arr[loc1].type;
//                     break;
//                 }
//                 loc1 -= 1;
//             }
//             if (type === "object") {
//                 valString = valString + space.repeat(arr[flag].lv - 1) + arr[flag].name + colon + singleQuotes + arr[flag].value + singleQuotes + comma + lineFeed;
//             }
//             if (type === "array") {
//                 valString = valString + space.repeat(arr[flag].lv - 1) + singleQuotes + arr[flag].value + singleQuotes + comma + lineFeed;
//             }
//             while (arr[loc].lv > arr[flag + 1].lv - 1 && loc) {
//                 if (arr[loc].lv < arr[flag].lv) {
//                     if (arr[loc].type === "array") {
//                         valString += space.repeat(arr[loc].lv - 1) + bracketR + lineFeed;
//                     }
//                     if (arr[loc].type === "object") {
//                         valString += space.repeat(arr[loc].lv - 1) + braceR + lineFeed;
//                     }
//                 }
//                 loc -= 1;
//             }
//         }



//         if (flag + 2 === arr.length) {
//             console.log("in");
//             let loc = flag + 1;
//             if (arr[loc].type === 'object') {
//                 valString = valString + space.repeat(arr[flag + 1].lv - 1) + arr[flag + 1].name + colon + '{}' + comma + lineFeed;

//             }else if(arr[loc].type === 'array'){
//                 valString = valString + space.repeat(arr[flag + 1].lv - 1) + arr[flag + 1].name + colon + '[]' + comma + lineFeed;
//             }else{
//                 valString = valString + space.repeat(arr[flag + 1].lv - 1) + arr[flag + 1].name + colon + singleQuotes + arr[flag + 1].value + singleQuotes + comma + lineFeed;
//             }

//             while (arr[loc].lv > 0 && loc) {
//                 let stFl = 0;
//                 for(let i = 0; i < stick.length; i++ ){
//                     if (loc ===  stick[i] - 1) {
//                         console.log(loc);
//                         loc -= 1;
//                         stFl = 1;
//                         break;
//                     }
//                 }
//                 if(stFl === 1){
//                     stFl = 0;
//                     continue;
//                 }  
//                 if (arr[loc].lv < arr[flag + 1].lv) {
//                     if (arr[loc].type === "array") {
//                         valString += space.repeat(arr[loc].lv - 1) + bracketR + lineFeed;
//                     }
//                     if (arr[loc].type === "object") {
//                         // console.log(stick);
//                         valString += space.repeat(arr[loc].lv - 1) + braceR + lineFeed;
//                     }
//                 }
//                 loc -= 1;
//             }



//         }
//         console.log(stick);

//         flag += 1;

//     }

//     return valString + lineFeed + braceR;

// }
